package com.manor.paradise.activity

import android.content.Intent
import android.view.View
import com.gyf.immersionbar.ktx.immersionBar
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.LybMyActiveDetailsAdapter
import com.manor.paradise.bean.AuraValueBean
import com.manor.paradise.bean.ReikInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.lyb_activity_my_active.*
import kotlinx.android.synthetic.main.lyb_activity_my_active.mTitleBarLayout

class LybMyActiveActivity : AbsActivity() {
    private val mList = arrayListOf<AuraValueBean.RecordsBean>()
    private val mAdapter by lazy {
        LybMyActiveDetailsAdapter(mList).apply {

        }
    }
    var pageNum = 1
    override fun getLayoutId() = R.layout.lyb_activity_my_active
    override fun main() {
        super.main()
        immersionBar {
            statusBarColor(R.color.white)
            titleBar(mTitleBarLayout)
            statusBarDarkFont(true)
        }
        setTopTitle("我的活跃度", rightIv = R.drawable.question_mark)
        mActiveDetailsRv.adapter = mAdapter
        mActiveDetailsSrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
        setData()
        initMode()
    }
    fun setData() {
        ElvesHttpUtil.getdetailsList(pageNum, httpCallBack)
    }
    private val httpCallBack = object : JHttpCallback() {
        override fun onSuccess(code: Int, msg: String, info: String) {
            if (code != 200) {
                ToastUtil.show(msg)
                return
            }
            if (pageNum == 1){
                mList.clear()
            }
            var auraValueBean: AuraValueBean = GsonUtil.gsonToBean<AuraValueBean>(info, AuraValueBean::class.java)
            mList.addAll(auraValueBean.records)
            KLog.e("json", auraValueBean)
            if (pageNum == 1) {
                mAdapter.notifyDataSetChanged()
                mActiveDetailsSrl.finishRefresh()
            } else {
                mAdapter.addData(auraValueBean.records)
                mActiveDetailsSrl.finishLoadMore()
            }
            if (mAdapter.data.isEmpty()) {
                mAdapter.setEmptyView(R.layout.view_empty_default)
            }
        }

        override fun onError() {
            super.onError()
        }
    }
    private fun initMode() {
        ElvesHttpUtil.getGetInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var reikindexBean: ReikInfoBean = GsonUtil.gsonToBean<ReikInfoBean>(info, ReikInfoBean::class.java)
                KLog.e("json", reikindexBean)
                mJichuActiveTv.text = reikindexBean.baseAnima.toString()
                mJiachengActiveTv.text = reikindexBean.additionAnima.toString()
                mFengxianActiveTv.text = reikindexBean.lossAnima.toString()
            }

            override fun onError() {
                super.onError()
            }
        })
    }
    override fun onDestroy() {
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_USER_ANIMA_LIST)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_GET_INFO)
        super.onDestroy()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.mRightIv ->{
                LybActiveExplainActivity.launch()
            }
        }
    }
}